<template>
  <div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label="商品 ID">
              <span>{{ props.row.id }}</span>
            </el-form-item>
            <el-form-item label="商品名称">
              <span>{{ props.row.name }}</span>
            </el-form-item>
            <el-form-item label="创建时间">
              <span>{{ props.row.ctime | format }}</span>
            </el-form-item>
            <el-form-item label="商品分类">
              <span>{{ props.row.category }}</span>
            </el-form-item>
            <el-form-item label="商品描述">
              <span>{{ props.row.goodsDesc }}</span>
            </el-form-item>
            <el-form-item label="商品价格">
              <span>{{ props.row.price }}</span>
            </el-form-item>
            <el-form-item label="商品销量">
              <span>{{ props.row.rating }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column label="商品名称" prop="id" width="120px">
      </el-table-column>
      <el-table-column label="所属分类" prop="name" width="200px">
      </el-table-column>
      <el-table-column label="商品价格" prop="price" width="100px">
      </el-table-column>
      <el-table-column label="商品图片" width="200px">
        <template v-slot="{ row }">
          <img
            v-if="row.imgUrl"
            class="goos-img"
            :src="beseUrl + row.imgUrl"
            alt=""
          />
        </template>
      </el-table-column>
      <el-table-column label="商品描述" prop="goodsDesc" width="250px">
      </el-table-column>
      <el-table-column label="操作" prop="desc" width="240px" v-slot="{ row }">
        <el-button type="primary" size="mini">编辑</el-button>
        <el-button type="danger" size="mini" @click="deladd(row.id)"
          >删除</el-button
        >
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
//
import { goodslist, delcommodity } from '@/api/Goods.js'
import moment from 'moment'
export default {
  data() {
    return {
      tableData: [],
      currentPage: 1,
      pageSize: 5,
      total: 0,
      beseUrl: 'http://127.0.0.1:5000/upload/imgs/goods_img/'
    }
  },
  created() {
    this.getlist()
  },
  methods: {
    async getlist() {
      const { data, total } = await goodslist({
        currentPage: this.currentPage,
        pageSize: this.pageSize
      })
      this.tableData = data
      this.total = total
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.getlist()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.getlist()
    },
    // 删除单个
    deladd(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const { code } = await delcommodity({ id })
          if (code === 0) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.getlist()
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  },
  filters: {
    format(str) {
      return moment(str).format('YYYY-MM-DD HH:mm:ss')
    }
  }
}
</script>

<style>
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
.goos-img {
  width: 50px;
  height: 50px;
}
</style>
